﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using ThuPhap.Models;
using System.Collections.ObjectModel;

namespace ThuPhap.Class
{
    public class SavePictureInfo
    {
        public SavePictureInfo()
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.DirectoryExists("SavedPictures") == false)//directory does not exist
            {
                //create SavedPictures directory
                storage.CreateDirectory("SavedPictures");
            }
        }

        public string SavedNewPiture(StoredItem StoredItem)
        {
            string filename = StoredItem.Name;
            string result = string.Empty;
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string name = string.Format("SavedPictures//{0}.tp", filename);
            //string name = string.Format("SavedPictures//{0}", filename);
            if (storage.FileExists(name))
            {
                storage.DeleteFile(name);
            }
            //if (storage.FileExists(string.Format("SavedPictures//{0}", filename)) == false)
            //{
                using (var storageFile = storage.CreateFile(name))
                {
                    var xmlSerializer = new XmlSerializer(typeof(StoredItem));
                    xmlSerializer.Serialize(storageFile, StoredItem);
                    storageFile.Close();
                }
                result = filename;
            //}
            //else
            //{
            //    result = string.Empty;
            //}
            
            return result;
        }

        public ObservableCollection<StoredItem> GetData()
        {
            //var storage = IsolatedStorageFile.GetUserStoreForApplication();
            var results = new ObservableCollection<StoredItem>();
            string[] filenames = this.GetFileNames();//storage.GetFileNames("SavedPictures//*.*");
            foreach (var item in filenames)
            {
                var data = this.GetDataFile(item);
                results.Add(data);
                
            }
            return results;
        }

        public string[] GetFileNames()
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string[] filenames = storage.GetFileNames("SavedPictures//*.*");
            if (filenames.Length > 0)
                Array.Sort(filenames, StringComparer.InvariantCulture);
            return filenames;
        }

        public StoredItem GetDataFile(string FileName)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "SavedPictures\\" + FileName;
            //string filename = "SavedPictures\\" + FileName;
            if (storage.DirectoryExists("SavedPictures") && storage.FileExists(filename))
            {
                using (var storageFile = storage.OpenFile(filename, System.IO.FileMode.Open))
                {
                    var xmlSerializer = new XmlSerializer(typeof(StoredItem));

                    return (StoredItem)xmlSerializer.Deserialize(storageFile);
                }
            }
            return null;
        }

        public void RemoveFile(string FileName)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "SavedPictures\\" + FileName;
            //string filename = "SavedPictures\\" + FileName;
            if (storage.DirectoryExists("SavedPictures") && storage.FileExists(filename))
            {
                storage.DeleteFile(filename);
            }
        }
    }
}
